<template>
  <div class="table">
    <div class="container">
      <div class="handle-box" >
        <!--<el-button type="primary" size="mini" @click="delAll">批量删除</el-button>-->
        <!--<el-input v-model="select_word" size="mini" placeholder="查询管理员（id,用户名，昵称）" class="handle-input"></el-input>-->
        <el-button type="primary" size="mini" @click="centerDialogVisible = true">添加管理员</el-button>
      </div>
    </div>
    <el-table class="el-table_p"  size="mini" ref="multipleTable" border :data="userList">
      <!--<el-table-column type="selection" ></el-table-column>-->
      <el-table-column prop="id" label="管理员id" align="center"></el-table-column>
      <el-table-column prop="account" label="管理员名称" align="center"></el-table-column>
      <el-table-column prop="nickName" label="昵称" align="center"></el-table-column>
      <el-table-column prop="lastLoginTime" label="上次登录时间" align="center"></el-table-column>
      <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--<div class="pagination">-->
    <!--<el-pagination-->
    <!--background-->
    <!--layout = "total,prev,pager,next"-->
    <!--:current-page="currentPage"-->
    <!--:page-size="pageSize"-->
    <!--:total="userList.length"-->
    <!--@current-change="handleCurrentChange"-->
    <!--&gt;-->
    <!--</el-pagination>-->
    <!--</div>-->

    <el-dialog title="添加管理员" :visible.sync="centerDialogVisible" width="440px" center>
      <el-form :model="registerForm" ref="registerForm" label-width="120px" :rules="rules">
        <el-form-item prop="account" label="管理员名称" size="mini">
          <el-input v-model="registerForm.account" placeholder="管理员名称"></el-input>
        </el-form-item>
        <el-form-item prop="pwd" label="密码" size="mini">
          <el-input type="password" v-model="registerForm.pwd" placeholder="密码"></el-input>
        </el-form-item>

        <el-form-item prop="nickName" label="昵称" size="mini">
          <el-input v-model="registerForm.nickName" placeholder="昵称"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer">
                <el-button size="mini" @click="centerDialogVisible = false">取消</el-button>
                <el-button size="mini" @click="addUser" type="primary">确定</el-button>
            </span>
    </el-dialog>
    <el-dialog title="修改管理员" :visible.sync="editVisible" width="440px" center>
      <el-form :model="form" ref="form" label-width="120px" :rules="rules">
        <el-form-item prop="account" label="管理员名称" size="mini">
          <el-input v-model="form.account" placeholder="管理员名称"></el-input>
        </el-form-item>
        <!--<el-form-item prop="pwd" label="原密码" size="mini">-->
        <!--<el-input type="password" v-model="form.pwd" placeholder="密码"></el-input>-->
        <!--</el-form-item>-->
        <!--<el-form-item prop="pwd1" label="新密码" size="mini">-->
        <!--<el-input type="password" v-model="form.pwd1" placeholder="密码"></el-input>-->
        <!--</el-form-item>-->
        <el-form-item prop="nickName" label="昵称" size="mini">
          <el-input v-model="form.nickName" placeholder="昵称"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
                <el-button size="mini" @click="editVisible = false">取消</el-button>
                <el-button size="mini" @click="editSave" type="primary">确定</el-button>
            </span>
    </el-dialog>
    <el-dialog title="删除管理员" :visible.sync="delVisible" width="40%" center>
      <div align="center" style="margin-bottom: 20px;color: red;font-weight: bold">删除不可恢复，是否确定删除？</div>
      <el-table class="el-table_p"  size="mini" ref="multipleTable" border :data="delMangeArr">
        <!--<el-table-column type="selection" ></el-table-column>-->
        <el-table-column prop="id" label="管理员id" align="center"></el-table-column>
        <el-table-column prop="account" label="管理员名称" align="center"></el-table-column>
        <el-table-column prop="nickName" label="昵称" align="center"></el-table-column>

      </el-table>
      <span slot="footer">
                <el-button size="mini" @click="delVisible = false">取消</el-button>
                <el-button size="mini" @click="deleteRow" type="primary">确定</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
  import {getUserList, addUser, updateUser, delUser} from '../../api/public/api';
  import { getNowFormatDate } from '../../units/time'
  import {mixin} from '../../../static/js/index';
  export default {
    mixins: [mixin],
    data() {
      return {
        level: 0,
        userList: [],
        registerForm: {      //添加框
          account: "",
          paw: "",
          nickName: "",

        },
        form: {      //编辑框
          account: "",
          pwd: "",
          pwd1: "",
          nickName: "",
        },
        id: "",


        centerDialogVisible: false, //添加弹窗是否显示
        editVisible: false,         //编辑弹窗是否显示
        delVisible: false,          //删除弹窗是否显示
        delMangeArr:[],


        rules: {
          account: [
            {required: true, message: '请输入用户名', trigger: 'blur'}
          ],
          pwd: [
            {required: true, message: '请输入密码', trigger: 'blur'}
          ],

          nickName: [
            {required: false, message: '请输入昵称', trigger: 'blur'}
          ]
        }
      }
    },


    created() {
      let data = {}
      this.getData(data);
      this.level = localStorage.getItem('level')
    },
    methods: {
      updateData() {
        let data = {}
        this.getData(data);
      },
      //查询所有管理员
      getData(data) {
        getUserList(data).then(res => {
          if (res.data.code == 0) {
            res.data.data.records.map(item => {
              item.lastLoginTime = getNowFormatDate(item.lastLoginTime)

            })
            this.userList = res.data.data.records
          }


        })
      },
      //添加管理员
      addUser() {

        this.$refs['registerForm'].validate(valid => {
          if (valid) {
            let params = {
              'account': this.registerForm.account,
              'nickName': this.registerForm.nickName,
              'pwd': this.registerForm.pwd
            };

            addUser(params)
              .then(res => {
                console.log(res)
                if (res.data.code == 0) {
                  this.centerDialogVisible = false;
                  this.notify("添加成功", "success");
                  this.updateData()
                } else {
                  this.notify("添加失败", "error");
                }
              })
              .catch(err => {
                console.log(err);
              });
            this.registerForm= {      //添加框
              account: "",
              paw: "",
              nickName: "",

            }
          }
        })

      },
      //弹出编辑页面
      handleEdit(row) {
        this.id = row.id
        this.editVisible = true;
        this.form = {      //编辑框
          account: row.account,
          pwd: row.pwd,
          nickName: row.nickName,
        }

      },
      //保存编辑页面修改的数据
      editSave() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            let params = {
              'id': this.id,
              'account': this.form.account,
              'nickName': this.form.nickName

            };

            updateUser(params).then(res => {
              if (res.data.code == 0) {
                this.editVisible = false;
                this.notify("修改成功", "success");
                this.updateData()
              } else {
                this.notify("修改失败", "error");
              }
            })
              .catch(err => {
                console.log(err);
              });

          }
        })

      },

      //删除一名用户
      handleDelete(row) {
        this.id = row.id
        this.delVisible = true
        this.delMangeArr=[]
        this.delMangeArr.push(row)
      },
      deleteRow() {

        delUser(this.id)
          .then(res => {
            if (res.data.code == 0) {
              this.delVisible = false;
              this.updateData()
              this.notify("删除成功", "success");
            } else {
              this.notify("删除失败", "error");
            }
          })
          .catch(err => {
            console.log(err);
          });

      },
      //转向该用户的收藏列表
      getCollect(id) {
        this.$router.push({path: '/collect', query: {id}})
      }
    }
  }
</script>

<style scoped>
  .handle-box {
    margin-bottom: 20px;
  }
  .center{

  }
</style>
